/* eslint-disable no-undef,no-param-reassign,import/named,consistent-return */
/**
 * Created by chengzhenfu on 2019/5/5.
 * Modified by  on .
 *axios 封装
 */
// import { Loading } from 'element-ui';
import baseURL from './basePath'
import Axios from 'axios';
import router from '@/router/index';
import utils from './utils'
const fetch = Axios.create({
  withCredentials: false,
  baseURL,
  timeout: 20000, // 设置超时时间
});
const time = null

// 请求发送前拦截，根据地址重新配置域名，再子路由请求时必须加/ 例如：请求 localhost:80/app/inset  fetch.get('/app/inset');
fetch.interceptors.request.use(
  function(config) {
    if (!vm.isLoading) {
		const whiteApi = ['/api/user/collect','/api/user/like','/api/school/modelList'];
		if(!whiteApi.includes(config.url)){
			utils.loadGif.showLoading();
		}
    }
	if(config.params){
		config.params.token = utils.getSession('token');
	}
	if(config.data){
		config.data.token = utils.getSession('token');
	}
    return config;
  },
  // 对请求错误做些什么
  function(error) {
    utils.loadGif.hideLoading();
    return Promise.reject(error)
  }
);

// 添加响应拦截器
fetch.interceptors.response.use(
  function(response) {
    utils.loadGif.hideLoading();
	if(response.data.code == "201"){
		utils.removeSession('token');
		router.push('/login');
		vm.$message.warning(response.data.message);
	}
	else{
		return response;
	}
  },
  function(error) {
    utils.loadGif.hideLoading();
    const {
      status,
      data
    } = error.response;
    switch (status) {
      case 201:
		utils.removeSession('token');
        router.push('/login');
        vm.$message.warning(data.error.message);
        break;
      case 500:
        vm.$message.error('系统繁忙');
        break;
      default:
        vm.$message.error('系统繁忙');
        return Promise.reject(data);
        break;
    }
  });

export default fetch;
